function max() {
    var iMax = arguments[0]

    for (var i = 0; i < arguments.length; i++) {
        if (iMax < arguments[i]) {
            iMax = arguments[i]
        }
    }
    return iMax
}

function min(){
    var iMin = arguments[0]
    for(var i = 1;i<arguments.length;i++){
        if(iMin>arguments[i]){
            iMin=arguments[i]
        }
    }
    return iMin
}

function sum() {
    var res = 0

    for (var i = 0; i < arguments.length; i++) {
        res += arguments[i]
    }

    return res
}

function mapArr(arr, n) {
    var newArr = []
    for (var i = 0; i < arr.length; i++) {
        newArr.push(arr[i] * n)
    }

    return newArr
}

function findArr(arr,n){
    for(var i = 0;i<arr.length;i++){
        if(n === arr[i]){
            return 'true'
    }
}
        return 'false'
}

function bubbleSort(arr) {
    for (var j = 0; j < arr.length - 1; j++) {
        for (var i = 0; i < arr.length - 1 - j; i++) {
            // 当前比下一个大，就交换位置
            if (arr[i] > arr[i + 1]) {
                // 交换位置
                var tmp = arr[i]
                arr[i] = arr[i + 1]
                arr[i + 1] = tmp
            }
        }
    }

    return arr
}

function selectSort(arr) {
    for (var j = 0; j < arr.length - 1; j++) {
        var maxIndex = j    // 循环找到最大值以后把它更改掉
        for (var i = j; i < arr.length; i++) {
            if (arr[i] > arr[maxIndex]) {
                maxIndex = i
            }
        }
        if (j !== maxIndex) {       // 优化自己和自己相等不去交换
            var tmp = arr[j]
            arr[j] = arr[maxIndex]
            arr[maxIndex] = tmp
        }
        console.log(arr)
    }
    return arr
}
// 对象转字符串
function queryStr(options) {
    // 准备一个数组
    var arr = []
    // 循环对象把拼接好的内容存到数组里面
    for (var key in options) {
        // console.log(key)
        // console.log(obj[key])
        // console.log(key + '=' + obj[key])
        // 存到数组里面
        arr.push(key + '=' + options[key])
    }
    // console.log(arr.join('&'))
    return arr.join('&')
}
//字符串转对象
function strQuery(str) {
    // 准备一个对象
    var obj = {}
    // 把字符串转成数组
    var arr = str.split('&')
    console.log(arr)
    // 循环数组把里面等号切掉
    arr.forEach(function (ele) {
        // console.log(ele.split('='))
        var newArr = ele.split('=')
        console.log(newArr)
        // console.log(newArr[0])      // 对象的键
        // console.log(newArr[1])      // 对象的值
        obj[newArr[0]] = newArr[1]
    })
    // console.log(obj)
    return obj
}
// 个位数加0
function toDou(n) {
    return n < 10 ? '0' + n : '' + n
}
//随机数
function random(n, m) {
    return parseInt(Math.random() * (m - n) + n)
}
//拖拽封装
function drag(obj) {
    obj.onmousedown = function (e) {
        var oldX = e.offsetX
        var oldY = e.offsetY
        document.onmousemove = function (e) {
            var x = e.pageX - oldX
            var y = e.pageY - oldY

            // 限制范围
            if (x <= 0) {
                x = 0
            } else if (x >= window.innerWidth - obj.offsetWidth) {
                x = window.innerWidth - obj.offsetWidth
            }

            if (y <= 0) {
                y = 0
            } else if (y >= window.innerHeight - obj.offsetHeight) {
                y = window.innerHeight - obj.offsetHeight
            }

            obj.style.left = x + 'px'
            obj.style.top = y + 'px'
        }
    }
    obj.onmouseup = function () {
        document.onmousemove = null
    }
}

let timer = null
function move(obj, attr, target, fn) {
    clearInterval(timer)
    timer = setInterval(function () {
        // 起点     getComputedStyle(元素)[样式名]获取非行间样式
        let start = parseFloat(getComputedStyle(obj)[attr])
        // 距离
        let cur = (target - start) / 5
        // 有可能出现小数，正数向上取整，负数向下取整
        cur = cur > 0 ? Math.ceil(cur) : Math.floor(cur)
        // 起点 + 距离 = 每次移动距离
        obj.style[attr] = start + cur + 'px'
        // 停止定时器
        if (start === target) {
            clearInterval(timer)
            // 所有复杂数据类型只能反向判断，不能等于 Boolean(fn) === true
            // if (fn != undefined) {
            //     fn()
            // }
        }
    }, 16)
}


function tick() {
    // 获取一个当前时间
    var now = new Date()
    var hours = now.getHours()

    // 判断是不是奇数，如果是就-1变成偶数
    if (hours % 2 !== 0) {
        hours--
    }
    oTime.innerHTML = toDou(hours) + ':00'

    // 倒计时
    // 未来时间
    var target = new Date()
    target.setHours(hours + 2, 0, 0, 0)  // 设置目标时间

    // 时间差
    var reduce = parseInt((target - now) / 1000)

    oH.innerHTML = toDou(parseInt(reduce / 3600))

    oM.innerHTML = toDou(parseInt(reduce % 3600 / 60))

    oS.innerHTML = toDou(reduce % 60)
}

function ajax(options) {
    // 1. 创建
    let xhr = new XMLHttpRequest()

    // 2. 连接
    if (options.type === 'get') {
        xhr.open('get', options.url + '?' + queryStr(options.data), true)
    }
    if (options.type === 'post') {
        xhr.open('post', options.url, true)
    }
    // 3. 发送
    if (options.type === 'get') {
        xhr.send()
    }
    if (options.type === 'post') {
        // 设置请求头
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(queryStr(options.data))
    }

    // 4. 接收
    xhr.onload = function () {
        // console.log(xhr.responseText)
        // console.log(options.success)
        // options.success(xhr.responseText)
        if (options.success) {
            options.success(xhr.responseText)
        }
    }
}

// 放大镜
class Enlarge {
    constructor(oEnlarge, imgObj) {
        this.enlarge = oEnlarge
        this.small = this.enlarge.querySelector('.small')
        this.mask = this.enlarge.querySelector('.mask')
        this.big = this.enlarge.querySelector('.big')
        this.smallImg = this.enlarge.querySelector('.small img')
        this.bigImg = this.enlarge.querySelector('.big img')
        this.btn = this.enlarge.querySelectorAll('.list li')

        this.imgObj = imgObj

        // small加事件
        this.addEvent()
    }

    addEvent() {
        this.small.onmouseover = () => {
            // 移入显示
            this.mask.classList.add('show')
            this.big.classList.add('show')
        }
        this.small.onmouseout = () => {
            // 移出隐藏
            this.mask.classList.remove('show')
            this.big.classList.remove('show')
        }
        this.small.onmousemove = (e) => {
            // offsetLeft 到定位父级元素的距离
            // console.log(this.enlarge.offsetLeft, this.enlarge.offsetTop)
            let x = e.pageX - this.enlarge.offsetLeft - this.mask.offsetWidth / 2
            let y = e.pageY - this.enlarge.offsetTop - this.mask.offsetHeight / 2

            // 限制范围
            if (x < 0) {
                x = 0
            } else if (x > this.small.clientWidth - this.mask.offsetWidth) {
                x = this.small.clientWidth - this.mask.offsetWidth
            }

            if (y < 0) {
                y = 0
            } else if (y > this.small.clientHeight - this.mask.offsetHeight) {
                y = this.small.clientHeight - this.mask.offsetHeight
            }

            this.mask.style.left = x + 'px'
            this.mask.style.top = y + 'px'

            // 比例：大 / 小
            this.bigImg.style.left = - x * (this.bigImg.offsetWidth / this.smallImg.offsetWidth) + 'px'
            this.bigImg.style.top = - y * (this.bigImg.offsetHeight / this.smallImg.offsetHeight) + 'px'
        }
        // 切换
        this.btn.forEach((ele, index) => {
            ele.onmousemove = () => {
                for (let i = 0; i < this.btn.length; i++) {
                    this.btn[i].classList.remove('on')
                }
                this.btn[index].classList.add('on')
                this.smallImg.src = this.imgObj.smallImg[index]
                this.bigImg.src = this.imgObj.bigImg[index]
            }
        })
    }
}